package com.demo.user.service.impl;

import com.demo.user.bo.UserMainPageBO;
import com.demo.user.converter.UserMainConverter;
import com.demo.user.entity.UserMain;
import com.demo.user.entity.table.UserMainTableDef;
import com.demo.user.exception.DemoUserException;
import com.demo.user.mapper.UserMainMapper;
import com.demo.user.service.UserMainService;
import com.demo.user.vo.UserMainVO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户服务实现
 *
 * @author DayDayUp
 * create on 2023-05-18
 **/
@Service
public class UserMainServiceImpl extends ServiceImpl<UserMainMapper,UserMain> implements UserMainService {
    @Resource
    private UserMainMapper userMainMapper;
    @Resource
    private UserMainConverter userMainConverter;

    @Override
    public Page<UserMainVO> pageUserMain(UserMainPageBO userMainPageBO) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (StringUtils.isNotBlank(userMainPageBO.getUsername())) {
            queryWrapper.where(UserMainTableDef.USER_MAIN.USERNAME.like(userMainPageBO.getUsername()));
        }
        Page<UserMain> paginate = userMainMapper.paginate(userMainPageBO.pageNumber, userMainPageBO.pageSize, queryWrapper);
        List<UserMain> records = paginate.getRecords();
        List<UserMainVO> userMainVOList = userMainConverter.toUserMainList(records);
        Page<UserMainVO> userMainVOPage = Page.of(paginate.getPageNumber(), paginate.getPageSize(), paginate.getTotalRow());
        userMainVOPage.setRecords(userMainVOList);
        return userMainVOPage;
    }
}
